﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text;
using Assist;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.Creation;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Revit;
using YArchitech.Revit.RevitExtension;
using YJKArch.Utils;
using YJKDragCurveJig;
using HYView;

namespace YJKArch.ElevationDim
{
	[Regeneration(RegenerationOption.Manual)]
	[Transaction(TransactionMode.Manual)]
	public class CmdElevationDim : YJKCommandManual
	{
		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		protected override Result YJKExcute(ExternalCommandData cmdData, ref string message, ElementSet elements)
		{
			CmdViewType types = CmdViewType.Elevation;
			if (SwitchView.SetUsableView(types, cmdData) != Autodesk.Revit.UI.Result.Succeeded)
			{
				return Autodesk.Revit.UI.Result.Failed;
			}
			CmdElevationDim.wndRevit = new WindowHandle(Process.GetCurrentProcess().MainWindowHandle);
			DlgElevationDim dlgElevationDim = new DlgElevationDim(cmdData);
			dlgElevationDim.Show(CmdElevationDim.wndRevit);
			int wMsg = 7;
			CmdElevationDim.SendMessage(CmdElevationDim.wndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
			Autodesk.Revit.Creation.Application create = this.m_app.Create;
			List<JigEdgeInfo> list = new List<JigEdgeInfo>();
			List<JigTextInfo> list2 = new List<JigTextInfo>();
			for (;;)
			{
				XYZ ptBase = null;
				if (dlgElevationDim.CreateJigData(this.m_doc.ActiveView, ref ptBase, ref list, ref list2))
				{
					XYZ ptInsert = null;
					if (!DragCurveJigInterface.GetInsertPosition(cmdData, list, false, list2, false, ptBase, ref ptInsert))
					{
						if (!dlgElevationDim.InfoChanged)
						{
							break;
						}
						dlgElevationDim.InfoChanged = false;
					}
					else if (dlgElevationDim.InfoChanged)
					{
						dlgElevationDim.InfoChanged = false;
					}
					else if (list.Count > 0)
					{
						Transaction transaction = new Transaction(this.m_doc, "CreateDim");
						transaction.Start();
						ElementSet eleSet = new ElementSet();
						this.CreateDim(list, list2, this.m_doc.ActiveView, ptInsert, ref eleSet);
						transaction.Commit();
						Transaction transaction2 = new Transaction(this.m_doc, "MakeGroup");
						transaction2.Start();
						RevitVersionFuncs.NewGroup(this.m_doc, eleSet);
						transaction2.Commit();
					}
				}
			}
			dlgElevationDim.Close();
			return base.YJKExcute(cmdData, ref message, elements);
		}

		private bool CreateDim(List<JigEdgeInfo> curveInfoList, List<JigTextInfo> textInfoList, Autodesk.Revit.DB.View view, XYZ ptInsert, ref ElementSet newgrpElements)
		{
			Transform identity = Transform.Identity;
			identity.Origin = ptInsert;
			identity.BasisX = view.RightDirection;
			identity.BasisY = view.UpDirection;
			identity.BasisZ = view.ViewDirection;
			for (int i = 0; i < curveInfoList.Count; i++)
			{
				JigEdgeInfo jigEdgeInfo = curveInfoList[i];
				XYZ startPoint = Geometry.TransformPoint(jigEdgeInfo.StartPoint, identity);
				XYZ endPoint = Geometry.TransformPoint(jigEdgeInfo.EndPoint, identity);
				Line line = YJKLineEx.YJKGetBound(startPoint, endPoint);
				if (!(null == line))
				{
					DetailCurve detailCurve = this.m_doc.Create.NewDetailCurve(view, line);
					newgrpElements.Insert(detailCurve);
					for (int j = 0; j < RevitVersionFuncs.LineStyles(this.m_doc, detailCurve).Size; j++)
					{
                        GraphicsStyle graphicsStyle = RevitVersionFuncs.LineStyles(this.m_doc, detailCurve).get_Item(j) as GraphicsStyle;
						int hashCode = -2000042.GetHashCode();
						if (graphicsStyle.GraphicsStyleCategory.GetHashCode() == hashCode)
						{
							detailCurve.LineStyle = graphicsStyle;
							break;
						}
					}
				}
			}
			foreach (JigTextInfo jigTextInfo in textInfoList)
			{
				double num = jigTextInfo.theTextNoteType.get_Parameter(BuiltInParameter.TEXT_SIZE).AsDouble();
				double num2 = jigTextInfo.theTextNoteType.get_Parameter(BuiltInParameter.TEXT_WIDTH_SCALE).AsDouble();
				double lineWidth = (double)Encoding.Default.GetBytes(jigTextInfo.StrText).Length * num * num2 * 1.5;
				XYZ origin = Geometry.TransformPoint(jigTextInfo.Origin, identity);
				XYZ baseVec = view.RightDirection;
				XYZ basisY = XYZ.BasisY;
				if (Convert.ToInt32(this.m_app.VersionNumber) > 2015)
				{
					baseVec = XYZ.BasisX;
				}
				TextNote textNode = this.m_doc.GetTextNode(view, origin, baseVec, basisY, lineWidth, jigTextInfo.TextAlign, jigTextInfo.StrText);
				if (textNode != null)
				{
					textNode.TextNoteType = jigTextInfo.theTextNoteType;
					newgrpElements.Insert(textNode);
				}
			}
			return true;
		}

		public static WindowHandle wndRevit;

		private ExternalCommandData m_Revit;
	}
}
